package com.youzikj.wx.core.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;


public class Utils {
	

	/**
	 * POST请求
	 */
	public static String post(String url, Map<String, String> paramsMap)
			throws IOException {
		if (url == null || "".equals(url))
			return "post request fail, not find url!";
		StringBuilder params = new StringBuilder();
		if (!paramsMap.isEmpty()) {
			for (Map.Entry<String, String> entry : paramsMap.entrySet())
				params.append(entry.getKey()).append("=").append(
						URLEncoder.encode(entry.getValue(), "utf-8")).append(
						"&");
			params.delete(params.length() - 1, params.length());
		}
		URL realUrl = new URL(url);
		URLConnection conn = realUrl.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		PrintWriter out = new PrintWriter(conn.getOutputStream());
		out.print(params.toString());
		out.flush();
		StringBuilder result = new StringBuilder();
		BufferedReader in = new BufferedReader(new InputStreamReader(conn
				.getInputStream()));
		int c = 0;
		while ((c = in.read()) != -1)
			result.append((char) c);
		out.close();
		in.close();
		return result.toString();
	}
	
	
	/**
	 * 
	 */



}
